<!-- 水质仪详情 -->
<template>
  <div class="water-details">
    <van-nav-bar :title="info.productName" left-arrow @click-left="() => $router.go(-1)" safe-area-inset-top>
      <template #left>
        <img style="width: 25px" src="../../assets/images/返回2.png" alt="" />
      </template>
    </van-nav-bar>
    <!-- 内容 -->
    <div class="content">
      <div class="top">
        <div>
          日期：<span @click="getTime('s')" style="width:250px">{{ content.stime }}</span> -
          <span @click="getTime('e')" style="width:250px">{{ content.etime }}</span>
        </div>
        <van-button class="btn" type="primary" @click="searchData" size="large">查询</van-button>
        <div style="margin-top:10px">设备编号：{{ info.dev_sn }}</div>
      </div>
      <div>
        <van-tabs v-model="active">
          <van-tab :title="item.name" v-for="(item, index) in properties" v-bind:key="index">
            <van-cell v-for="(a,index) in list" :key="index">
              {{a.value}} <a style="float:right">{{a.time}}</a>
            </van-cell>

            <van-pagination v-model="page" :total-items="count" @change="getData" :items-per-page="rows" />



          </van-tab>
        </van-tabs>

      </div>


    </div>

    <van-overlay :show="show">
      <div class="wrapper" @click.stop>
        <div class="block">
          <div class="time">{{ time }}</div>
          <van-datetime-picker :show-toolbar="false" ref="picker" v-model="pickertime" confirm-button-text=" "
            cancel-button-text=" " @change="change" type="datetime" />
          <div class="btns">
            <van-button class="cancel" plain type="default" size="small" @click="show = false">取消</van-button>
            <van-button class="config" plain type="info" size="small" @click="config">确定</van-button>
          </div>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>

import { sensorData, sensorHistoryData, sensorInfo, sensorHistorySimData } from "../../api/sensor";
export default {
  name: "",
  data() {
    return {
      active: 0,
      show: false,
      time: this.$moment(new Date()).format("YYYY-MM-DD HH:MM:SS"),
      pickertime: "",
      content: {
        stime: "请选择",
        etime: "请选择",
      },
      type: '',
      info: {},
      list: [],
      properties: [],
      page: 1,
      rows: 10,
      count: 0,
    };
  },
  watch: {
    active: {
      handler(newValue, oldValue) {
        // console.log('obj changed', newValue, oldValue)
        this.type = this.properties[newValue].id;
        this.page = 1;
        this.getData();
      }
    }
  },
  methods: {
    getTime(e) {
      this.show = true;
      this.type = e
    },
    onClick() {

      console.log("event.detail.name");
    },
    config(e) {
      if (this.type == "s") {
        this.content.stime = this.time;
      } else {
        this.content.etime = this.time;
      }
      this.show = false;
    },
    change(val, value) {
      this.time = this.$moment(this.pickertime).format("YYYY-MM-DD HH:MM:SS");
      console.log(this.time);
    },
    async getInfo() {
      console.log("!!!");
      let res = await sensorInfo({ code: this.$route.query.code });
      if (res.status == 200) {
        var info = res.data;
        this.info = info;

        var metadata = JSON.parse(info.metadata);
        var properties = metadata.properties;

        // console.log("properties", properties);

        this.properties = properties;

        if (properties.length > 0) {

          this.type = properties[0].id;
          this.getData();
        }

      }
    },
    async getData() {


      var code = this.$route.query.code;

      var stime = "";
      var etime = "";
      if (this.content.stime != "请选择") {
        stime = this.content.stime;
      }
      if (this.content.etime != "请选择") {
        etime = this.content.etime;
      }

      let res = await sensorHistorySimData({
        userid: localStorage.getItem("userid"),
        code: code, stime: stime, etime: etime, type: this.type, page: this.page, rows: this.rows
      });

      console.log("res", res);
      if (res.status == 200) {
        var info = res.data;
        this.list = info.list;

        this.count = info.pageNum;



      }
    },
    async searchData() {

      this.page = 1;

      this.getData();


    }
  },
  computed: {},

  created() {
    this.getInfo();

    //  this.getData();
  },
  mounted() { },
};
</script>
<style lang="scss">
.water-details {
  .van-datetime-picker {
    height: 400px;
    overflow: hidden;
  }
}
</style>
<style lang="scss" scoped>
.water-details {
  width: 100%;
  padding-bottom: 120px;
  position: relative;

  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }

  .block {
    margin-top: -200px;
    width: 90%;
    height: 600px;
    border-radius: 15px;
    background-color: #fff;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;

    .time {
      line-height: 50px;
      font-size: 26px;
      text-align: center;
    }

    .btns {
      margin-top: 20px;
      height: 100px;
      width: 100%;
      display: flex;
      justify-content: space-around;
      align-items: center;

      .cancel,
      .config {
        width: 100px;
      }
    }
  }

  .content {
    width: 94%;
    margin: 20px auto 0;
    background-color: #fff;
    border-radius: 15px;
    padding: 30px;
    font-size: 24px;

    .top {
      margin-bottom: 10px;

      > :first-child {
        margin-bottom: 20px;

        span {
          border: 1px solid #ccc;
          padding: 8px;
        }
      }
    }

    .progress {
      margin-top: 50px;

      > :first-child {
        margin-bottom: 20px;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>